#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "student.h"

// --------------------- 添加学生 ---------------------
void addStudent() {
    FILE *fp = fopen("students.txt", "a+");
    if (!fp) {
        printf("文件打开失败！\n");
        return;
    }

    Student stu;
    printf("请输入学号: ");
    scanf("%s", stu.id);
    printf("请输入姓名: ");
    scanf("%s", stu.name);
    printf("请输入年龄: ");
    scanf("%d", &stu.age);
    printf("请输入成绩: ");
    scanf("%f", &stu.score);

    fprintf(fp, "%s %s %d %.2f\n", stu.id, stu.name, stu.age, stu.score);
    printf("学生添加成功！\n");
    fclose(fp);
}

// --------------------- 删除学生 ---------------------
void deleteStudent() {
    FILE *fp = fopen("students.txt", "r");
    FILE *temp = fopen("temp.txt", "w");
    if (!fp || !temp) {
        printf("文件打开失败！\n");
        return;
    }

    char target[10];
    Student stu;
    int found = 0;
    printf("请输入要删除的学号: ");
    scanf("%s", target);

    while (fscanf(fp, "%s %s %d %f", stu.id, stu.name, &stu.age, &stu.score) != EOF) {
        if (strcmp(stu.id, target) != 0)
            fprintf(temp, "%s %s %d %.2f\n", stu.id, stu.name, stu.age, stu.score);
        else
            found = 1;
    }

    fclose(fp);
    fclose(temp);
    remove("students.txt");
    rename("temp.txt", "students.txt");

    if (found) printf("删除成功！\n");
    else printf("未找到该学号！\n");
}

// --------------------- 查询学生 ---------------------
void queryStudent() {
    FILE *fp = fopen("students.txt", "r");
    if (!fp) {
        printf("文件打开失败！\n");
        return;
    }

    char target[30];
    Student stu;
    int found = 0;
    printf("请输入学号或姓名: ");
    scanf("%s", target);

    while (fscanf(fp, "%s %s %d %f", stu.id, stu.name, &stu.age, &stu.score) != EOF) {
        if (strcmp(stu.id, target) == 0 || strcmp(stu.name, target) == 0) {
            printf("学号:%s 姓名:%s 年龄:%d 成绩:%.2f\n", stu.id, stu.name, stu.age, stu.score);
            found = 1;
        }
    }
    if (!found) printf("未找到该学生！\n");
    fclose(fp);
}

// --------------------- 修改学生 ---------------------
void modifyStudent() {
    FILE *fp = fopen("students.txt", "r");
    FILE *temp = fopen("temp.txt", "w");
    if (!fp || !temp) {
        printf("文件打开失败！\n");
        return;
    }

    char target[10];
    Student stu;
    int found = 0;
    printf("请输入要修改的学号: ");
    scanf("%s", target);

    while (fscanf(fp, "%s %s %d %f", stu.id, stu.name, &stu.age, &stu.score) != EOF) {
        if (strcmp(stu.id, target) == 0) {
            found = 1;
            printf("输入新的姓名: ");
            scanf("%s", stu.name);
            printf("输入新的年龄: ");
            scanf("%d", &stu.age);
            printf("输入新的成绩: ");
            scanf("%f", &stu.score);
        }
        fprintf(temp, "%s %s %d %.2f\n", stu.id, stu.name, stu.age, stu.score);
    }

    fclose(fp);
    fclose(temp);
    remove("students.txt");
    rename("temp.txt", "students.txt");

    if (found) printf("修改成功！\n");
    else printf("未找到该学号！\n");
}

// --------------------- 系统菜单 ---------------------
void menu() {
    int choice;
    while (1) {
        printf("\n--- 学生管理系统 ---\n");
        printf("1. 添加学生\n2. 删除学生\n3. 查询学生\n4. 修改学生\n5. 退出\n选择操作: ");
        scanf("%d", &choice);
        getchar();

        switch (choice) {
            case 1: addStudent(); break;
            case 2: deleteStudent(); break;
            case 3: queryStudent(); break;
            case 4: modifyStudent(); break;
            case 5: return;
            default: printf("输入无效！\n");
        }
    }
}